﻿using System.Collections.Concurrent;

namespace housingsafety.server.Caching.Core
{
    public class CacheFactory
    {
        private readonly ConcurrentDictionary<string, ICache> m_Caches = new ConcurrentDictionary<string, ICache>();
        private readonly ICacheProvider m_CacheProvider;

        public CacheFactory(ICacheProvider cacheProvider)
        {
            m_CacheProvider = cacheProvider;
        }

        public ICache GetCache(string name = null)
        {
            var cache = m_Caches.GetOrAdd(name ?? string.Empty, n => CreateCache(n));
            return cache;
        }

        private ICache CreateCache(string name)
        {
            return m_CacheProvider.CreateCache(name);
        }
    }
}
